package com.link.ppt.Utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.link.ppt.App.App;
import com.link.ppt.Consts.Constants;
import com.link.ppt.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by liucxu on 2017/11/7.
 */

public class ShareDialogUtils {
    private  static View shareView;
    private  Dialog dialog;
    private  String web;
    private static ShareDialogUtils instance;

    public static ShareDialogUtils init() {
        if(instance==null){
            instance=new ShareDialogUtils();
        }
        shareView=null;
        return instance;
    }

    public void share(Context context, String url) {
        web=url;
        if (shareView == null) {
            shareView = View.inflate(context, R.layout.dialog_share_layout, null);
            initViews(shareView);
            initVotedShareDialog(context);
        }
        dialog.show();
    }

    private void initViews(View share_view) {
        LinearLayout wechat_circle_layout_share = (LinearLayout) share_view.findViewById(R.id.wechat_circle_layout_share);
        LinearLayout we_chat_layout_share = (LinearLayout) share_view.findViewById(R.id.we_chat_layout_share);
        LinearLayout close_dialog_layout_share_share = (LinearLayout) share_view.findViewById(R.id.close_dialog_layout_share_share);
        close_dialog_layout_share_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        wechat_circle_layout_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                shareToWechat(true);
            }
        });
        we_chat_layout_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                shareToWechat(false);
            }
        });
    }

    private void initVotedShareDialog(Context context) {
        dialog = new Dialog(context, R.style.Theme_Light_Dialog);
        //获得dialog的window窗口
        Window window = dialog.getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setCancelable(false);
        dialog.setContentView(shareView);
    }

    private void shareToWechat(boolean isTimeLine) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = web;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "爱解梦";
        msg.description = "爱解梦是一个专注于利用人工智能记录梦境解析梦境的App";
        Bitmap bmp = BitmapFactory.decodeResource(App.GetInstance().getResources(), R.mipmap.ic_launcher);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
        bmp.recycle();
        msg.thumbData = FileUtils.bmpToByteArray(thumbBmp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("share_app");
        req.message = msg;
        if(isTimeLine){
            req.scene=SendMessageToWX.Req.WXSceneTimeline;
        }
        else{
            req.scene=SendMessageToWX.Req.WXSceneSession;
        }
        WXAPIFactory.createWXAPI(App.GetInstance(), Constants.WEIXIN_APPID).sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
